﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace PhanMemSapThoiKhoaBieu
{
    public partial class FormPhanCongGiangDay : Form
    {
        public FormPhanCongGiangDay()
        {
            InitializeComponent();
        }
        //Load danh sách giáo viên
        public void GetAllGiaoVien()
        {
            int index = 0;
            DataTable dt = PhanCongGiangDayBUS.GetAllGiaoVien();
            dgvDanhSachGiaoVien.Rows.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                dgvDanhSachGiaoVien.Rows.Add();
                DataGridViewRow dgr = dgvDanhSachGiaoVien.Rows[index];
                dgr.Cells["STT"].Value = index + 1;
                dgr.Cells["ID"].Value = dr[0].ToString();
                dgr.Cells["TenGiaoVien"].Value = dr[1].ToString();
                dgr.Cells["TenLoaiGiaoVien"].Value = dr[2];
                index++;
            }
            dgvDanhSachGiaoVien.AllowUserToAddRows = false;
        }
        //Load lên combobox
        public void GetAllLoaiGiaoVien()
        {
            DataTable dt = PhanCongGiangDayBUS.GetAllLoaiGiaoVien();
            cbLoaiGiaoVien.DataSource = dt;
            cbLoaiGiaoVien.ValueMember = "ID";
            cbLoaiGiaoVien.DisplayMember = "TenLoaiGiaoVien";
            DataTable lop = PhanCongGiangDayBUS.GetAllLop();
            cbLop.DataSource = lop;
            cbLop.ValueMember = "ID";
            cbLop.DisplayMember = "TenLop";
        }
        //hiển thị lên textbox và combobox
        public void HienThiGiaoVien()
        {
            int index = dgvDanhSachGiaoVien.CurrentRow.Index;
            DataGridViewRow dgr = dgvDanhSachGiaoVien.CurrentRow;
            tbHoTen.Text = dgr.Cells[2].Value.ToString();
            cbLoaiGiaoVien.Text = dgr.Cells[3].Value.ToString();
            txtMaGV.Text = dgr.Cells[1].Value.ToString();
        }
        //load lên girldview những lớp mà giáo viên đó dạy
        public void LoadLopHoc(string ID)
        {
            int index = 0;
            DataTable dt = PhanCongGiangDayBUS.GetAllGiaoVienTheoLop(ID);
            dgvDanhSachLopPhuTrach.Rows.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                dgvDanhSachLopPhuTrach.Rows.Add();
                DataGridViewRow dgr = dgvDanhSachLopPhuTrach.Rows[index];
                // dgr.Cells["STT"].Value = index + 1;
                dgr.Cells["TenLop"].Value = dr[0].ToString();
                cbLop.Text = dr[0].ToString();
                index++;
            }
            dgvDanhSachLopPhuTrach.AllowUserToAddRows = false;
        }
        //load lên girldview những lớp đó mà giáo viên không dạy
        public void LoadLopHocKhongDay(string ID)
        {
            int index = 0;
            DataTable dt = PhanCongGiangDayBUS.GetAllGiaoVienLop(ID);
            dgvDanhSachLop.Rows.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                dgvDanhSachLop.Rows.Add();
                DataGridViewRow dgr = dgvDanhSachLop.Rows[index];
                // dgr.Cells["STT"].Value = index + 1;
                dgr.Cells["TenLopKhong"].Value = dr[0].ToString();
                index++;
            }
            dgvDanhSachLop.AllowUserToAddRows = false;
        }

       

        private void FormPhanCongGiangDay_Load(object sender, EventArgs e)
        {
            GetAllGiaoVien();
            GetAllLoaiGiaoVien();
        }

        private void dgvDanhSachLopPhuTrach_Click(object sender, EventArgs e)
        {
            HienThiGiaoVien();
            int index = dgvDanhSachGiaoVien.CurrentRow.Index;
            DataGridViewRow dgr = dgvDanhSachGiaoVien.CurrentRow;
            string id = dgr.Cells[1].Value.ToString();
            LoadLopHoc(id);
            LoadLopHocKhongDay(id);
        }

        private void btnLayThem_Click(object sender, EventArgs e)
        {

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                GiaoVienDTO gv = new GiaoVienDTO();
                gv.ID = txtMaGV.Text.ToString();
                gv.HoTen = tbHoTen.Text.ToString();
                gv.IDLoaiGiaoVien = cbLoaiGiaoVien.SelectedValue.ToString();
                bool kq = PhanCongGiangDayBUS.Them(gv);
                if (kq)
                {
                    MessageBox.Show("Thêm giáo viên thành công");
                    GetAllGiaoVien();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Thêm giáo viên thất bại");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                string maGV = txtMaGV.Text.ToString();
                bool kq = PhanCongGiangDayBUS.Xoa(maGV);
                if (kq)
                {
                    MessageBox.Show("Delete thành công");
                    GetAllGiaoVien();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Xóa thất bại");
            }
        }

        private void btnCapNhatGiaoVien_Click(object sender, EventArgs e)
        {
            try
            {
                GiaoVienDTO gv = new GiaoVienDTO();
                gv.ID = txtMaGV.Text.ToString();
                gv.HoTen = tbHoTen.Text.ToString();
                gv.IDLoaiGiaoVien = cbLoaiGiaoVien.SelectedValue.ToString();
                bool kq = PhanCongGiangDayBUS.CapNhat(gv);
                if (kq)
                {
                    MessageBox.Show("Update giáo viên thành công");
                    GetAllGiaoVien();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Update giáo viên thất bại");
            }
        }
    }
}
